//
//  NSString+RCAES.m
//  StructToolsDemo
//
//  Created by 杨晓辉 on 2017/8/22.
//  Copyright © 2017年 杨晓辉. All rights reserved.
//

#import "NSString+RCAES.h"
#import "NSData+RCAES.h"
@implementation NSString (RCAES)
/**
 AES(CBC模式)加密
 @param key 秘钥
 @param offset 偏移量
 @param sourceStr 待加密的数据
 @return 加密后的结果
 */
+ (NSString *)CBCEncryptAESWithKey:(NSString*)key andOffset:(NSString*)offset andSourceStr:(NSString*)sourceStr;
{
    NSData * encryptData = [ NSData CBCEncryptAESWithKey:key andOffset:offset andSourceData:[sourceStr dataUsingEncoding:NSUTF8StringEncoding]];
    NSString * encryptStr =[encryptData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return encryptStr;
}

/**
 AES(CBC模式)解密
 @param key 秘钥
 @param offset 偏移量
 @param sourceStr 待解密的数据
 @return 解密后的结果
 */
+ (NSString *)CBCDecryptAESWithKey:(NSString*)key andOffset:(NSString*)offset andSourceStr:(NSString*)sourceStr;
{
    NSData * encryptData = [[NSData alloc] initWithBase64EncodedString:sourceStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
   
    NSData *decryptData = [NSData CBCDecryptAESWithKey:key andOffset:offset andSourceData:encryptData];
    
    

    NSString *decryptStr = [[NSString alloc] initWithData:decryptData encoding:NSUTF8StringEncoding];

    return decryptStr;
}


/**
 AES(ECB模式)加密
 @param key 秘钥
 @param sourceStr 待加密的数据
 @return 加密后的结果
 */
+ (NSString *)ECBEncryptAESWithKey:(NSString*)key andSourceStr:(NSString*)sourceStr;
{
    NSData *encryptData = [NSData ECBEncryptAESWithKey:key andSourceData:[sourceStr dataUsingEncoding:NSUTF8StringEncoding]];
    NSString * encryptStr =[encryptData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return encryptStr;

}

/**
 AES(ECB模式)解密
 @param key 秘钥
 @param sourceStr 待解密的数据
 @return 解密后的结果
 */
+ (NSString *)ECBDecryptAESWithKey:(NSString*)key andSourceStr:(NSString*)sourceStr
{
    NSData * encryptData = [[NSData alloc] initWithBase64EncodedString:sourceStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
    
    NSData *decryptData = [NSData ECBDecryptAESWithKey:key andSourceData:encryptData];
    
    
    
    NSString *decryptStr = [[NSString alloc] initWithData:decryptData encoding:NSUTF8StringEncoding];

    return decryptStr;

}

@end
